package daily;

/**
 * @author MtRider
 * @version 1.0
 * @description 旋转数组
 * @GiteeAndGithub MtRider
 * @date 2022/4/21 17:33
 */

import java.util.Arrays;

/**
 * 输入: nums = [1,2,3,4,5,6,7], k = 3
 * 输出: [5,6,7,1,2,3,4]
 * 解释:
 * 向右轮转 1 步: [7,1,2,3,4,5,6]
 * 向右轮转 2 步: [6,7,1,2,3,4,5]
 * 向右轮转 3 步: [5,6,7,1,2,3,4]
 */
public class demo03 {
    public static void main(String[] args) {
        int[] nums = {1, 2};
        int k = 3;
        //rotate(nums, k);
        rotate3(nums, k);
    }

    //时间复杂度过高n^2
    public static void rotate(int[] nums, int k) {
        if (nums.length == 1 || nums.length == 0) {
            return;
        }
        int temp = 0;
        for (int j = 0; j < k; j++) {
            for (int i = nums.length - 1; i > 0; i--) {
                if (i == nums.length - 1) {
                    temp = nums[i];
                }
                nums[i] = nums[i - 1];
            }
            nums[0] = temp;
        }
        for (int num : nums) {
            System.out.print(num + " ");
        }
    }

    //临时数组取模
    public void rotate2(int[] nums, int k) {
        int length = nums.length;
        int temp[] = new int[length];
        //把原数组值放到一个临时数组中，
        for (int i = 0; i < length; i++) {
            temp[i] = nums[i];
        }
        //然后在把临时数组的值重新放到原数组，并且往右移动k位
        for (int i = 0; i < length; i++) {
            nums[(i + k) % length] = temp[i];
        }

    }

    //字符串concat
    public static void rotate3(int[] nums, int k) {
        String numsString = "";
        String test="";
        for (int num : nums) {
            numsString += num;
        }
        String substring = numsString.substring(0, k+1);
        String substring1 = numsString.substring(k+1, numsString.length());
        test=test.concat(substring1+substring);
        System.out.println(test);
        for (int i = 0; i < nums.length; i++) {
            nums[i]=Integer.parseInt(String.valueOf(test.charAt(i)));
        }
    }
}
